package com.longrise.filedownloaderdemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.filedownloader.DownloadManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xkj on 2016/3/11.
 * 添加缓存的adapter
 */
public class AddDownLoadAdapter extends BaseExpandableListAdapter {

    private final DownloadManager mDownloadManager;
    private Context mContext;
    private List<List<Downloaditembean>> mList = new ArrayList<>();

    public AddDownLoadAdapter(Context context, DownloadManager downloadManager) {
        this.mContext = context;
        this.mDownloadManager = downloadManager;
    }

    public void setData(List<List<Downloaditembean>> list){
        mList.clear();
        mList.addAll(list);
        this.notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mList != null) {
            return mList.get(groupPosition).size();
        } else {
            return 0;
        }

    }

    @Override
    public Object getGroup(int groupPosition) {
       return null;

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mList != null) {
            return mList.get(groupPosition).get(childPosition);
        } else {
            return null;
        }

    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder parentHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_adddownload_title, null);
            parentHolder = new ParentHolder();
            parentHolder.mViewLine = convertView.findViewById(R.id.adddownload_top_line);
            parentHolder.mTv = (TextView) convertView.findViewById(R.id.adddownload_parent_tv);
            parentHolder.mIv = (ImageView) convertView.findViewById(R.id.adddownload_parent_iv);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }

        if (isExpanded) {
            parentHolder.mIv.setImageResource(R.drawable.icon_up);
        } else {
            parentHolder.mIv.setImageResource(R.drawable.icon_down);
        }

        if (groupPosition == 0) {
            parentHolder.mViewLine.setVisibility(View.GONE);
        } else {
            parentHolder.mViewLine.setVisibility(View.VISIBLE);
        }



        parentHolder.mTv.setText(groupPosition+"title");

        return convertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_adddownload_child, null);
            childHolder = new ChildHolder();
            childHolder.mTvtitle = (TextView) convertView.findViewById(R.id.adddownload_child_tv_title);
            childHolder.mIv = (ImageView) convertView.findViewById(R.id.adddownload_child_iv);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        if (mList != null) {
            Downloaditembean downloaditembean = mList.get(groupPosition).get(childPosition);
            String name = downloaditembean.getName();
            boolean checked = downloaditembean.isChecked();
            childHolder.mTvtitle.setText(name);
            if(downloaditembean.isDownloaded()){
                childHolder.mIv.setImageResource(R.drawable.finish);
            }else if(checked){
                childHolder.mIv.setImageResource(R.drawable.circle_check);
            }else{
                childHolder.mIv.setImageResource(R.drawable.circle);
            }

        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ParentHolder {
        private View mViewLine;

        private TextView mTv;

        private ImageView mIv;
    }

    class ChildHolder {
        private TextView mTvtitle;
        private ImageView mIv;
    }
}
